{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<form class="layui-form p-4" lay-filter="casecauseForm">
    {if condition="isset($detail) && $detail['id'] eq 0"}
    <h3 class="pb-3">添加案由信息</h3>
    {else}
    <h3 class="pb-3">编辑案由信息</h3>
    {/if}
    <table class="layui-table layui-table-form">
        <tr>
            <td class="layui-td-gray">案由名称<font>*</font></td>
            <td>
                <input type="text" name="name" value="{$detail['name']|default=''}" lay-verify="required" autocomplete="off" placeholder="请输入案由名称" lay-reqText="请输入案由名称" class="layui-input">
            </td>
        </tr>
        <tr>
            <td class="layui-td-gray">父级案由</td>
            <td>
                <div class="layui-input-block" style="margin-left: 0;">
                    <select name="parent_id" lay-filter="parentSelect" class="layui-select">
                        <option value="0">顶级案由</option>
                        {volist name="parentOptions" id="item"}
                        <option value="{$item.id}" {if condition="isset($detail) && $detail['parent_id'] eq $item.id"}selected{/if}>{$item.name}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-word-aux">选择案由的父级分类</div>
            </td>
        </tr>
        <tr>
            <td class="layui-td-gray">排序</td>
            <td>
                <input type="number" name="sort" value="{$detail['sort']|default=100}" autocomplete="off" placeholder="请输入排序值，值越小越靠前" class="layui-input">
            </td>
        </tr></table>
    <div class="py-3">
        <input type="hidden" name="id" value="{$detail['id']|default='0'}">
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
</form>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = [];
    function gouguInit() {
        var form = layui.form,
            tool = layui.tool, 
            layer = layui.layer;

        // 渲染表单
        form.render();

        // 监听表单提交
        form.on('submit(webform)', function(data) {
            layer.confirm('确定提交吗？', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                var loadIndex = layer.load(2, {shade: [0.1, '#fff']});
                $.post(window.location.href, data.field, function(res) {
                    layer.close(loadIndex);
                    //根据返回结果显示对应的提示信息和图标
                    layer.msg(res.msg, {
                        icon: res.code === 0 ? 1 : 2,  // code为0时显示成功图标(1)，否则显示错误图标(2)
                        time: res.code === 0 ? 1000 : 2000
                    }, function() {
                        if (res.code === 0) {
                            // 成功后关闭当前弹窗并刷新父页面
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            // 调用父页面的重新渲染方法
                            parent.reloadTable && parent.reloadTable();
                        }
                    });
                });
            });
            return false;
        });

        // 重置按钮事件
        $('button[type="reset"]').click(function() {
            if ({$detail['id'] | default='0'} == 0) {
                $('select[name="parent_id"]').val(0);
                form.render('select');
            }
        });
    }
</script>

{/block}
<!-- /脚本 -->
